<template>
	<view class="content">
		<view>
			<input type="text" v-model="title" placeholder="标题">
		</view>
		<view>
			<input type="text" v-model="content" placeholder="内容">
		</view>
		<view>
			<button type="primary" size="mini" @click="to_submit()">提交</button>
		</view>
	</view>
</template>

<script>
	import config from '../../lib/config.js'
	export default {
		data() {
			return {
				title: '',
				content: '',
			}
		},
		methods: {
			to_submit: function() {
				let title = this.title.trim();
				let content = this.content.trim();
				if (!title || !content) {
					uni.showToast({
						title: '标题和内容不能为空',
						duration: 2000,
						icon: 'none',
					});
					return;
				}
				uni.request({
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					},
					url: config.api_url+'?action=add',
					data: {
						title: this.title,
						content: this.content
					},
					success: (res) => {
						let d = res.data;
						console.log(d);
						if (d.code == 0) {
							uni.showToast({
								title: '操作成功',
								duration: 1000
							});
							uni.navigateTo({
								url: '../lst/lst'
							})
						} else {
							uni.showToast({
								title: d.msg,
								duration: 3000
							})
						}
					}
				})
			}
		}
	}
</script>

<style>

</style>
